/**
 * Copyright 2009. Felix Dorner.
 * This code is licensed under the Eclipse Public License v1.0
 * http://www.eclipse.org/org/documents/epl-v10.html
 *
 * $Id$
 */
package com.objectcarpentry.kaylee.impl;

import java.net.URI;

import com.objectcarpentry.kaylee.IPlayableFilter;
import com.objectcarpentry.kaylee.IPlayer;
import com.objectcarpentry.kaylee.IPlayerEvent;
import com.objectcarpentry.kaylee.IPlayerListener;
import com.objectcarpentry.kaylee.KayleePackage;
import com.objectcarpentry.kaylee.PlayableState;
import com.objectcarpentry.kaylee.Playlist;
import com.objectcarpentry.kaylee.PlaylistEntry;
import com.objectcarpentry.kaylee.Track;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.notify.Notifier;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EcoreUtil;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Playlist Entry</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.objectcarpentry.kaylee.impl.PlaylistEntryImpl#getTrack <em>Track</em>}</li>
 *   <li>{@link com.objectcarpentry.kaylee.impl.PlaylistEntryImpl#getPlaylist <em>Playlist</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class PlaylistEntryImpl extends PlayableImpl implements PlaylistEntry {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public static final String copyright = "Copyright 2009. Felix Dorner.\nThis code is licensed under the Eclipse Public License v1.0\nhttp://www.eclipse.org/org/documents/epl-v10.html";

	/**
	 * The cached value of the '{@link #getTrack() <em>Track</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTrack()
	 * @generated
	 * @ordered
	 */
	protected Track track;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected PlaylistEntryImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return KayleePackage.Literals.PLAYLIST_ENTRY;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Track getTrack() {
		if (track != null && track.eIsProxy()) {
			InternalEObject oldTrack = (InternalEObject)track;
			track = (Track)eResolveProxy(oldTrack);
			if (track != oldTrack) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, KayleePackage.PLAYLIST_ENTRY__TRACK, oldTrack, track));
			}
		}
		return track;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Track basicGetTrack() {
		return track;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setTrack(Track newTrack) {
		Track oldTrack = track;
		track = newTrack;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, KayleePackage.PLAYLIST_ENTRY__TRACK, oldTrack, track));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Playlist getPlaylist() {
		if (eContainerFeatureID() != KayleePackage.PLAYLIST_ENTRY__PLAYLIST) return null;
		return (Playlist)eContainer();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetPlaylist(Playlist newPlaylist, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newPlaylist, KayleePackage.PLAYLIST_ENTRY__PLAYLIST, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setPlaylist(Playlist newPlaylist) {
		if (newPlaylist != eInternalContainer() || (eContainerFeatureID() != KayleePackage.PLAYLIST_ENTRY__PLAYLIST && newPlaylist != null)) {
			if (EcoreUtil.isAncestor(this, newPlaylist))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newPlaylist != null)
				msgs = ((InternalEObject)newPlaylist).eInverseAdd(this, KayleePackage.PLAYLIST__ELEMENTS, Playlist.class, msgs);
			msgs = basicSetPlaylist(newPlaylist, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, KayleePackage.PLAYLIST_ENTRY__PLAYLIST, newPlaylist, newPlaylist));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case KayleePackage.PLAYLIST_ENTRY__PLAYLIST:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetPlaylist((Playlist)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case KayleePackage.PLAYLIST_ENTRY__PLAYLIST:
				return basicSetPlaylist(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
		switch (eContainerFeatureID()) {
			case KayleePackage.PLAYLIST_ENTRY__PLAYLIST:
				return eInternalContainer().eInverseRemove(this, KayleePackage.PLAYLIST__ELEMENTS, Playlist.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case KayleePackage.PLAYLIST_ENTRY__TRACK:
				if (resolve) return getTrack();
				return basicGetTrack();
			case KayleePackage.PLAYLIST_ENTRY__PLAYLIST:
				return getPlaylist();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case KayleePackage.PLAYLIST_ENTRY__TRACK:
				setTrack((Track)newValue);
				return;
			case KayleePackage.PLAYLIST_ENTRY__PLAYLIST:
				setPlaylist((Playlist)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case KayleePackage.PLAYLIST_ENTRY__TRACK:
				setTrack((Track)null);
				return;
			case KayleePackage.PLAYLIST_ENTRY__PLAYLIST:
				setPlaylist((Playlist)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case KayleePackage.PLAYLIST_ENTRY__TRACK:
				return track != null;
			case KayleePackage.PLAYLIST_ENTRY__PLAYLIST:
				return getPlaylist() != null;
		}
		return super.eIsSet(featureID);
	}

	@Override
	public void playOn(final IPlayer player, Object parameters) {
		getTrack().eAdapters().add(new Adapter(){
			public Notifier getTarget() {
				return null;
			}
			public boolean isAdapterForType(Object type) {
				return false;
			}
			public void notifyChanged(Notification notification) {
				if (notification.getFeature() == KayleePackage.Literals.PLAYABLE__STATE){
					PlayableState ps = (PlayableState) notification.getNewValue();
					if (ps == PlayableState.STOPPED) {
						getTrack().eAdapters().remove(this);
					}
					setState(ps);
				}
			}
			public void setTarget(Notifier newTarget) {}
		});
		getTrack().playOn(player, parameters);
	}

	@Override
	public boolean match(IPlayableFilter filter) {
		return getTrack().match(filter);
	}


} //PlaylistEntryImpl
